//
//  ExamPlayInfoModel.m
//  student
//
//  Created by JCai on 2018/10/22.
//  Copyright © 2018年 higer. All rights reserved.
//

#import "ExamPlayInfoModel.h"


@implementation ExamPlayJudgment


@end

@implementation ExamPlayInfo


+ (NSDictionary *)mj_objectClassInArray
{
    return @{@"judgmentItems" : @"ExamPlayJudgment"
             };
}
@end

@implementation ExamPlayInfoModel

+ (NSDictionary *)mj_objectClassInArray
{
    return @{@"playInfo" : @"ExamPlayInfo"
             };
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.playInfoRate = [[NSMutableArray alloc] init];
        self.refreshRate = 1.0f;
        self.videoLength = 0;
    }
    return self;
}

- (void)dataOperate
{
    [self.playInfoRate removeAllObjects];
    
    ExamPlayInfo *beginPlayInfo = [self.playInfo firstObject];
    ExamPlayInfo *endPlayInfo = [self.playInfo lastObject];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSTimeInterval late1 = [[formatter dateFromString:beginPlayInfo.time] timeIntervalSince1970];
    NSTimeInterval late2 = [[formatter dateFromString:endPlayInfo.time] timeIntervalSince1970];
    self.videoLength = late2 - late1;
    
    NSMutableArray *indexArr = [[self.playInfo valueForKeyPath:@"time"] valueForKeyPath:@"@distinctUnionOfObjects.self"];
    
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
    NSArray *sortArray = [NSArray arrayWithObjects:descriptor, nil];
    NSArray *sortIndexArr = [indexArr sortedArrayUsingDescriptors:sortArray];
    
    NSMutableArray *playInfoRate = [[NSMutableArray alloc] init];
    for (NSString *indexStr in sortIndexArr) {
        NSMutableArray *playInfoArray =[NSMutableArray array];
        for (ExamPlayInfo *playInfo in self.playInfo) {
            if ([indexStr isEqualToString:playInfo.time]) {
                [playInfoArray addObject:playInfo];
            }
        }
    
        [playInfoRate addObject:playInfoArray];
    }

    CGFloat rate = (238590 - 104714) / 3384.0f;
    CGPoint lastPoint = CGPointZero;
    for (NSInteger i=0; i<playInfoRate.count; i++) {
        NSArray *playInfoArray = [playInfoRate objectAtIndex:i];
        ExamPlayInfo *playInfo = nil;
        if (playInfoArray.count == 1) {
            playInfo = [playInfoArray firstObject];
        }
        else if (playInfoArray.count > 1) {
            playInfo = [[ExamPlayInfo alloc] init];
            NSString *time = nil;   //
            NSArray *judgmentItems = nil;  //
            CGFloat angle = 0;  //
            NSInteger pox = 0;  //
            NSInteger poy = 0;  //
            for (ExamPlayInfo *playInfo in playInfoArray) {
                if (!time) {
                    time = playInfo.time;
                }
                if (!judgmentItems) {
                    judgmentItems = playInfo.judgmentItems;
                }
                angle += playInfo.angle;
                pox += playInfo.pox;
                poy += playInfo.poy;
            }
            playInfo.time = time;   //
            playInfo.judgmentItems = judgmentItems ? judgmentItems : [NSArray array];  //
            playInfo.angle = angle / (CGFloat)playInfoArray.count;  //
//            playInfo.index;  //
            playInfo.pox = pox / playInfoArray.count;  //
            playInfo.poy = poy / playInfoArray.count;  //
            
        }
        if (playInfo) {
            CGFloat pox = (playInfo.pox - 104714) / rate;
            CGFloat poy = (playInfo.poy - 92444) / rate;
            if (i == 0) {
                lastPoint = CGPointMake(pox, poy);
            }
            else {
                playInfo.pox = pox;  //
                playInfo.poy = poy;  //
                UIBezierPath *path = [UIBezierPath bezierPath];
                [path setLineWidth:1];
                [path moveToPoint:lastPoint];
                
                lastPoint = CGPointMake(pox, poy);
                [path addLineToPoint:lastPoint];
                playInfo.path = path;
                [self.playInfoRate addObject:playInfo];
            }
        }
    }
    
    if (self.videoLength <= self.playInfoRate.count) {
        self.videoLength = self.playInfoRate.count;
        self.refreshRate = 1.0f;
    }
    else if (self.videoLength > self.playInfoRate.count) {
//        self.refreshRate = 1.0f;
//        self.videoLength = self.playInfoRate.count;
    }
}

@end
